package com.fitness.dao;
import com.fitness.entity.Consumption;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 消费记录表数据访问对象，继承 BaseDAO，实现消费记录相关数据库操作
 */
public class ConsumptionDAO extends BaseDAO {
    /**
     * 插入消费记录（使用触发器自动填充字段）
     */
    public boolean insertConsumption(Consumption consumption) {
        String sql = "INSERT INTO 消费记录表(会员ID, 项目ID, 消费时间) " +
                "VALUES (?, ?, ?)";
        int affectedRows = executeUpdate(sql,
                consumption.getMemberId(),
                consumption.getProjectId(),
                consumption.getConsumeTime());
        return affectedRows > 0;
    }

    /**
     * 根据消费ID删除记录
     */
    public boolean deleteConsumptionById(int consumptionId) {
        String sql = "DELETE FROM 消费记录表 WHERE 消费ID = ?";
        int affectedRows = executeUpdate(sql, consumptionId);
        return affectedRows > 0;
    }

    /**
     * 查询所有消费记录
     */
    public List<Consumption> getAllConsumptions() {
        List<Consumption> consumptionList = new ArrayList<>();
        String sql = "SELECT * FROM 消费记录表";
        ResultSet rs = executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                Consumption consumption = new Consumption();
                consumption.setConsumptionId(rs.getInt("消费ID"));
                consumption.setMemberId(rs.getInt("会员ID"));
                consumption.setProjectId(rs.getInt("项目ID"));
                consumption.setManagerId(rs.getInt("经理ID"));
                consumption.setConsumeTime(rs.getTimestamp("消费时间"));
                consumption.setOriginalAmount(rs.getDouble("原价金额"));
                consumption.setDiscountAmount(rs.getDouble("折扣金额"));
                consumption.setActualAmount(rs.getDouble("实际金额"));
                consumptionList.add(consumption);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(null, null, rs);
        }
        return consumptionList;
    }
}